package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.constants.CopyWriterConstant;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.CopyWriterConfigUtil;
import com.ytjj.qmyx.admin.dao.ProductAttributeDao;
import com.ytjj.qmyx.admin.dao.ProductSkuKeyDao;
import com.ytjj.qmyx.admin.dao.ProductSkuValueDao;
import com.ytjj.qmyx.admin.model.request.ProductAttributeRequest;
import com.ytjj.qmyx.admin.service.ProductAttributeService;
import com.ytjj.qmyx.mall.mapper.ProductAttributeKeyMapper;
import com.ytjj.qmyx.mall.mapper.ProductAttributeValueMapper;
import com.ytjj.qmyx.mall.model.ProductAttributeKey;
import com.ytjj.qmyx.mall.model.ProductAttributeKeyExample;
import com.ytjj.qmyx.mall.model.ProductAttributeValue;
import com.ytjj.qmyx.mall.model.ProductAttributeValueExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProductAttributeServiceImpl implements ProductAttributeService {

    @Resource
    ProductAttributeDao productAttributeDao;

    @Resource
    ProductAttributeKeyMapper productAttributeKeyMapper;

    @Resource
    ProductAttributeValueMapper productAttributeValueMapper;

    @Resource
    ProductSkuKeyDao productSkuKeyDao;

    @Resource
    ProductSkuValueDao productSkuValueDao;

    @Resource
    private CopyWriterConfigUtil copyWriterConfigUtil;

    @Override
    public CommonPage<ProductAttributeKey> keyList(ProductAttributeRequest productAttributeRequest) {
        PageHelper.startPage(productAttributeRequest.getPageNum(),productAttributeRequest.getPageSize());
        List<ProductAttributeKey> page = productAttributeDao.keyList(productAttributeRequest);
        return CommonPage.restPage(page);
    }

    @Override
    public List<ProductAttributeKey> keysAll() {
        return productAttributeKeyMapper.selectByExample(new ProductAttributeKeyExample());
    }

    @Override
    public int createKey(ProductAttributeKey productAttributeKey) {
        productAttributeKey.setCreateTime(new Date());
        return productAttributeKeyMapper.insert(productAttributeKey);
    }

    @Override
    public int updateKey(ProductAttributeKey productAttributeKey) {
        productAttributeKey.setUpdateTime(new Date());
        return productAttributeKeyMapper.updateByPrimaryKeySelective(productAttributeKey);
    }

    @Override
    public ProductAttributeKey getKey(Integer id) {
        return productAttributeKeyMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteKey(Integer id) {
        // 查看商品是否有用到这个属性
        Integer count = productSkuKeyDao.selectCount(id);
        if(count>0){
            throw new ApiException(copyWriterConfigUtil.getCopyWriterValue(CopyWriterConstant.ATTRIBUTE_CANT_DELETE));
        }
        // 删除所有的 value 属性
        ProductAttributeValueExample productAttributeValueExample = new ProductAttributeValueExample();
        productAttributeValueExample.createCriteria().andAttributeIdEqualTo(id);
        List<ProductAttributeValue> productAttributeValues = productAttributeValueMapper.selectByExample(productAttributeValueExample);
        if(!CollectionUtils.isEmpty(productAttributeValues)){
            for(ProductAttributeValue v : productAttributeValues){
                productAttributeValueMapper.deleteByPrimaryKey(v.getId());
            }
        }

        return productAttributeKeyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CommonPage<ProductAttributeValue> valueList(ProductAttributeRequest productAttributeRequest) {
        PageHelper.startPage(productAttributeRequest.getPageNum(),productAttributeRequest.getPageSize());
        List<ProductAttributeValue> page = productAttributeDao.valueList(productAttributeRequest);
        return CommonPage.restPage(page);
    }

    @Override
    public List<ProductAttributeValue> valuesAll() {
        return productAttributeValueMapper.selectByExample(new ProductAttributeValueExample());
    }

    @Override
    public int createValue(ProductAttributeValue productAttributeValue) {
        productAttributeValue.setCreateTime(new Date());
        return productAttributeValueMapper.insert(productAttributeValue);
    }

    @Override
    public int updateValue(ProductAttributeValue productAttributeValue) {
        productAttributeValue.setUpdateTime(new Date());
        return productAttributeValueMapper.updateByPrimaryKeySelective(productAttributeValue);
    }

    @Override
    public ProductAttributeValue getValue(Integer id) {
        return productAttributeValueMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteValue(Integer id) {
        // 查看商品是否有用到这个属性
        Integer count = productSkuValueDao.selectCount(id);
        if(count>0){
            throw new ApiException(copyWriterConfigUtil.getCopyWriterValue(CopyWriterConstant.ATTRIBUTE_CANT_DELETE));
        }
        return productAttributeValueMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ProductAttributeValue> getValueBykey(Integer key) {
        ProductAttributeValueExample productAttributeValueExample = new ProductAttributeValueExample();
        productAttributeValueExample.createCriteria().andAttributeIdEqualTo(key);
        return productAttributeValueMapper.selectByExample(productAttributeValueExample);
    }
}
